<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>
    <div id="div1">{{msg}}</div>
    <div id="div2">{{msg}}</div>
    <div id="num1">{{number}}</div>
  </div>
  <script>
    const data = {
      msg: 'Hello',
      number: 1
    }
    // 订阅者 - 观察者 - div1,div2
    class Watcher {
      constructor(name) {
        this.name = name
      }
      update() {
        console.log(this.name, ':我去更新DOM元素了')
      }
    }
    // 发布者-目标 通知div1,div2更新
    class Dep {
      constructor() {
        this.divs = []
      }
      addSub(div) {
        this.divs.push(div)
      }
      notify() {
        this.divs.forEach(div => {
          div.update()
        })
      }
    }

    const div1 = new Watcher('d1')
    const div2 = new Watcher('d2')
    const num1 = new Watcher('num1')
    const msg = new Dep()
    const number = new Dep()
    msg.addSub(div1)
    msg.addSub(div2)
    number.addSub(num1)
    msg.notify()
    number.notify()


  </script>
</body>

</html>